/**
 * Created by zhuoy on 2017/6/27.
 * 屏幕工具类
 * ui设计基准,iphone 6+ 1080p()   16:9
 * width:750 1920
 * height:1334 1080
 */

/*
 设备的像素密度，例如：
 PixelRatio.get() === 1          mdpi Android 设备 (160 dpi)
 PixelRatio.get() === 1.5        hdpi Android 设备 (240 dpi)
 PixelRatio.get() === 2          iPhone 4, 4S,iPhone 5, 5c, 5s,iPhone 6,xhdpi Android 设备 (320 dpi)
 PixelRatio.get() === 3          iPhone 6 plus , xxhdpi Android 设备 (480 dpi)
 PixelRatio.get() === 3.5        Nexus 6       */

 import {
    Dimensions,
    PixelRatio,
} from 'react-native';

// const BASE_WIN_HEIGHT = 1080;
// const BASE_WIN_WIDTH = 1920;
// const currentPixel = PixelRatio.get()  // 当前为iPhone6，值为2
// // 根据实际屏幕尺寸转换对应的像素高度
// const currentWidth = Dimensions.get('window').width;   // 当前设备宽度
// const currentHeight = Dimensions.get('window').height;    // 当前设备高度
// const pixelRatio6 = 2;      // 返回iPhone6的像素密度
// const fontScale = PixelRatio.getFontScale();  // 字体大小缩放比例
// const scale = Math.min(currentHeight / BASE_WIN_HEIGHT, currentWidth / BASE_WIN_WIDTH);

// export function getHeight(h) {
//     if (!this.height) {
//           const {height, width} = Dimensions.get('window');
//           this.height = height;
//           this.width = width;
//     }
//       return h / currentPixel * (this.height / BASE_WIN_HEIGHT);
// }

// // 根据实际屏幕尺寸转换对应的像素宽度
// export function getWidth(w) {
//       if (!this.width) {
//             const {height, width} = Dimensions.get('window');
//             this.height = height;
//             this.width = width;
//       }
//       return w / currentPixel * (this.width / BASE_WIN_WIDTH);
// }
// export function setText (val) {
//     val = (val * scale) * currentPixel / fontScale;
//     return val / pixelRatio6;
// }


let screenW = Dimensions.get('window').width;
let screenH = Dimensions.get('window').height;
let fontScale = PixelRatio.getFontScale();
let pixelRatio = PixelRatio.get();
// 高保真的宽度和告诉
const designWidth = 1920;
const designHeight = 1080;

// 根据dp获取屏幕的px
let screenPxW = PixelRatio.getPixelSizeForLayoutSize(screenW);
let screenPxH = PixelRatio.getPixelSizeForLayoutSize(screenH);

/**
 * 设置text
 * @param size  px
 * @returns {Number} dp
 */
export function setSpText(size) {
    console.log("screenW======"+screenW)
    console.log("screenPxW======"+screenPxW)
    var scaleWidth = screenW / designWidth;
    var scaleHeight = screenH / designHeight;
    var scale = Math.min(scaleWidth, scaleHeight);
    size = Math.round(size * scale/fontScale + 0.5);
    return size;
}

/**
 * 设置高度
 * @param size  px
 * @returns {Number} dp
 */
export function scaleSizeH(size) {
    var scaleHeight = size * screenPxH / designHeight;
    size = Math.round((scaleHeight / pixelRatio + 0.5));
    return size;
}

/**
 * 设置宽度
 * @param size  px
 * @returns {Number} dp
 */
export function scaleSizeW(size) {
    var scaleWidth = size * screenPxW / designWidth;
    size = Math.round((scaleWidth/pixelRatio + 0.5));
    return size;
}